
class Puzzle {

    // variables again, easier to make ROWS and COLS
    private static final int ROWS = 3;
    private static final int COLS = 3;

    private pieceTest[][] board;
    private pieceTest     emptyPiece;



    public Puzzle() {
        board = new pieceTest[ROWS][COLS];
        scramble(); // try scrambling things should go here
    }               // need to make a scramble method


    // needed to return the piece
    String getPiece(int row, int col) {
        return board[row][col].getPiece(); // can use the get piece method from pieceTest class
    } // awwesoommeee


    // Scramble area
    public void scramble() { // scramble should move the pieces around randomly
        for (int r=0; r<ROWS; r++) { // testing
            for (int c=0; c<COLS; c++) { // testing
                board[r][c] = new pieceTest(r, c, "" + (r*COLS+c+1)); // testing
            }
        }
        //needed to make an empty spot
        emptyPiece = board[ROWS-1][COLS-1]; // took away
        emptyPiece.setPiece(null);

        //scrambling the actual pieces around
        for (int r=0; r<ROWS; r++) {
            for (int c=0; c<COLS; c++) { // vvvvvvvvvvv swap piece used here vvvvvvvvvvvvvv
                swapPiece(r, c, (int)(Math.random()*ROWS), (int)(Math.random()*COLS)); // had to use random
            }
        }
    }

    // swap pieces method, actually swapping
    private void swapPiece(int r1, int c1, int r2, int c2) {
        pieceTest holdThese = board[r1][c1];
        board[r1][c1] = board[r2][c2]; // row 1, column 1 and row 2 and column 2 swap pieces
        board[r2][c2] = holdThese;
    }

    // need to make some sort of null check to use in the move piece method
        //Checking to see if there is an actual empty space next to the current piece
    // to do NULLCHECK HERE vvvvvvvvvvvvv
    private boolean nullCheck(int r, int c, int rTest, int cTest) {
        int rowNext = r + rTest;
        int colNext = c + cTest;
        // variable names

        // empty check and swap the pieces
        if (okToMoveRC(rowNext, colNext)&& board[rowNext][colNext] == emptyPiece) {
            swapPiece(r, c, rowNext, colNext);
            return true; // return true
        }
        return false; // false
    }


    // moving the actual piece around, needed to use
    public boolean movePiece(int r, int c) {
        // had to check if the piece was allowed to move or not, took a while.
        // worked with a friend to try and come to a solution
        return nullCheck(r, c, -1, 0) || nullCheck(r, c, 1, 0)|| nullCheck(r, c, 0, -1) || nullCheck(r, c, 0, 1);
        //
    }


  // ok to move code vvvvvvvvvv
    public boolean okToMoveRC(int r, int c) { // RC stands for row / column
        return r>=0 && r<ROWS && c>=0 && c<COLS;
    }


}




